package com.lg.online_shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.online_shop.entity.*;
import com.lg.online_shop.mapper.*;
import com.lg.online_shop.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/good")
public class GoodController {

    @Resource
    private GoodCategoryMapper goodCategoryMapper;

    @Resource
    private GoodMapper goodMapper;

    @Resource
    private SizeMapper sizeMapper;

    @Resource
    private ColorMapper colorMapper;

    @Resource
    private OrderMapper orderMapper;

    // 获取所有分类
    @GetMapping("/allCategory")
    public Result<List<GoodCategory>> getAllCategory() {
        List<GoodCategory> goodCategoryList = goodCategoryMapper.selectList(null);
        return Result.success(goodCategoryList);
    }

    // 获取某分类下的推荐商品(按价格升序)
    @GetMapping("/recommend")
    public Result<IPage<Good>> getRecommendGoods(
            @RequestParam Long categoryId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Good> pageParam = new Page<>(page, size);
        IPage<Good> goodPage = goodMapper.selectGoodsByCategoryWithPagination(categoryId, pageParam);
        return Result.success(goodPage);
    }

    // 获取某分类下的最受欢迎商品(按评分降序)
    @GetMapping("/popular")
    public Result<IPage<Good>> getPopularGoods(
            @RequestParam Long categoryId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Good> pageParam = new Page<>(page, size);
        IPage<Good> goodPage = goodMapper.selectPopularGoodsByCategory(pageParam, categoryId);
        return Result.success(goodPage);
    }

    // 获取商品详情
    @GetMapping("/detail")
    public Result<Good> getGoodDetail(@RequestParam Long goodId) {
        Good good = goodMapper.selectById(goodId);
        return Result.success(good);
    }

    // 搜索商品
    @GetMapping("/search")
    public Result<IPage<Good>> searchGoods(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Good> pageParam = new Page<>(page, size);
        IPage<Good> goodPage = goodMapper.selectPageByKeywordAndFields(pageParam, keyword);

        // 按评分排序
        goodPage.getRecords().sort((o1, o2) -> (int) o2.getGStar());

        return Result.success(goodPage);
    }

    // 通过 goodId 获取尺寸
    @GetMapping("/size")
    public Result<List<Size>> getSizeByGoodId(@RequestParam Long goodId) {
        List<Size> sizeList = sizeMapper.selectSizeByGoodId(goodId);
        return Result.success(sizeList);
    }

    // 通过 goodId 获取颜色
    @GetMapping("/color")
    public Result<List<Color>> getColorByGoodId(@RequestParam Long goodId) {
        List<Color> colorList = colorMapper.selectColorByGoodId(goodId);
        return Result.success(colorList);
    }

    // 修改商品信息
    @GetMapping("/update")
    public Result updateGood(Good good) {
        try {
            goodMapper.updateById(good);
            return Result.success("修改成功");
        } catch (Exception e) {
            return Result.error("修改失败");
        }
    }


    //    简单推荐算法推荐商品
    @GetMapping("/recommendations")
    public Result getRecommendedGoods() {
        // 获取所有商品，并按照评分和购买次数进行排序
        List<Good> goods = goodMapper.selectList(null).stream()
                .sorted((g1, g2) -> {
                    int starComparison = Float.compare( g2.getGStar(), g1.getGStar());
                    if (starComparison != 0) {
                        return starComparison;
                    }
                    // 可以从订单表统计购买次数
                    Long g1Count = orderMapper.selectCount( new QueryWrapper<Orders>().eq("g_id", g1.getId()));
                    Long g2Count = orderMapper.selectCount( new QueryWrapper<Orders>().eq("g_id", g2.getId()));
                    int purchaseCountComparison = Long.valueOf(g2Count).compareTo(Long.valueOf(g1Count));
                    return purchaseCountComparison;
                })
                .limit(10) // 取前 10 个推荐商品
                .toList();
        return  Result.success(goods);
    }
}